function FilterController() {

    this.mostrarMensajeAlerta= function() {
        alert("=== filter-controles.mostrarMensajeAlerta");
    };


    this.loadMyCities = function () {
        alert("========= in loadMyCities - alex");
        var selected = $("#provinceSelect option:selected");
        alert("========= in loadMyCities - alex selected="+selected);
        var output = "";
        alert("====== You Selected Province val="+selected.val()+ ", text=" + selected.text());
        if(selected.val() != 0){
			output = "====== You Selected Province id="+selected.val()+ ", " + selected.text();
		}
		$("#outputDebug").html(output);

        alert("=========== antes del JSON - loadMyCities");
        $.getJSON("findCityByProvince.dahtml",     // url
         { provinceId: selected.val() },   // request params
         function(json){           // callback
            var options = '';
            alert("entro al json Province -- City");
            options += '<option value="" selected="selected">Seleccione una Ciudad</option>'

            $(json.list.City).each(function() {
                options += '<option value="' + this.id + '">' + this.name + '</option>';
            });

             $("select#citySelect").html(options);

             $("#outputDebug").html(options);
            // ======================= Otra forma de popular el select
            // var options = '';
            // for (var i = 0; i < j.length; i++) {
            // options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            // }
            // $("select#ctlPerson").html(options);
         }
      );
      alert("=========== despues del JSON - loadMyCities");
    }

    this.loadMyRegions = function () {
        alert("========= in loadMyRegions - alex");
        var selected = $("#citySelect option:selected");
        alert("========= in loadMyRegions - alex selected="+selected);
		var output = "";
        alert("========= in loadMyRegions - alex selected.val()="+selected.val());
		if(selected.val() != 0){
			output = "====== You Selected City id="+selected.val()+ ", " + selected.text();
            alert("=========== antes del JSON - loadMyRegions");
            $.getJSON("findRegionByCity.dahtml",     // url
             { cityId: selected.val() },   // request params
             function(json){           // callback
                var options = '';
                 alert("entro al json Region -- City");
                $(json.list.Region).each(function() {
                    options += '<option value="' + this.id + '">' + this.name + '</option>';
                });

                 $("select#regionSelect").html(options);

                 $("#outputDebug").html(options);
                // ======================= Otra forma de popular el select
                // var options = '';
                // for (var i = 0; i < j.length; i++) {
                // options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                // }
                // $("select#ctlPerson").html(options);

             }
            );
            alert("=========== despues del JSON - loadMyRegions");
        }
		$("#outputDebug").html(output);
    }

    this.loadCities= function() {
        alert("=== filter-controles.loadCities");
        var provinceId = $("#provinceId").attr('value');
        alert("provinceId="+provinceId);
    };

     this.doLogin= function() {
	 	var user = $("#username").attr('value');
	   	var pass = $("#pin").attr('value');
        $("#recuadroMensaje").removeClass("ocultar");
        $("#recuadroMensaje").addClass("alerta");
        $("#recuadroMensaje").html("Ingresando...");
        
        if(user == "" || pass == "") {
            $("#recuadroMensaje").html("Debe ingresar el usuario y la clave.");
            //$("#recuadroMensaje").fadeOut("slow");
        } else {	             

		 	var url = "doLogin.htm?userName=" + user + "&password=" + pass;
		 	// El correlationId se setea en la funcion pollFor
		 	var pollUrl = "pollLogin.htm?userName="+ user +"&correlationId=";
		 	
		 	var callback = new LoginCallBack();
		 	linkController.asyncCall(url, pollUrl,callback);
	    }
     };

    // pre-submit callback for advancedFilterForm
    this.sendToAdvancedFilterForm= function (formData, jqForm) {
        alert('======= sendToAdvancedFilterForm - About to submit: \n\n' + $.param(formData));
        //return true;
    }

    // post-submit callback for advancedFilterForm
    this.showResponseAdvancedFilterForm= function (json, statusText)  {
        alert('======= en la funcion -showResponseAdvancedFilterForm-');

        alert('this: ' + this.tagName +
            '\nstatus: ' + statusText +
            '\n\njson: \n' +
            json + '\n\nThe output div should have already been updated with the responseText.');
        //$('#contenedorAddNewClubForm').hide('fast');

        var myHTML = "";

        index=1;


        myHTML += '<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">';
        myHTML += '<caption>ssssssssssssssssssss  </caption>';
        myHTML += '  <tr>';
        myHTML += '    <th scope="col" abbr="Configurations" class="nobg">Clubs</th>';
        myHTML += '    <th scope="col" abbr="Dual 1.8">ID</th>';
        myHTML += '    <th scope="col" abbr="Dual 2.5">E-mail</th>';
        myHTML += '    <th scope="col" abbr="Dual 2.5">Mas Detalles</th>';
        myHTML += '  </tr>';

        $(json.list.Club).each(function() {
            myHTML += ', id: ' + this.id + ', name' + this.name + ', mail:' + this.mail;
            index++;
            if (index % 2) {
                myHTML += '<tr>';
                myHTML += '  <th scope="row" abbr="Model" class="spec">' + this.name + '</th>';
                myHTML += '  <td>' + this.id + '</td>';
                myHTML += '  <td>' + this.mail+'</td>';
                myHTML += '  <td> <span class="gensmall"><a href="#" onclick="clubDetailController.showClubDetail(\''+ this.id +'\');return false;" class="gensmall">detalles</a></span></td>';
                myHTML += '</tr>';
            } else {
                myHTML += '<tr>';
                myHTML += '  <th scope="row" abbr="Model" class="specalt">' + this.name + '</th>';
                myHTML += '  <td class="alt">' + this.id + '</td>';
                myHTML += '  <td class="alt">' + this.mail+'</td>';
                myHTML += '  <td> <span class="gensmall"><a href="#" onclick="clubDetailController.showClubDetail(\''+ this.id +'\');return false;" class="gensmall">detalles</a></span></td>';
                myHTML += '</tr>';
            }
        });

        myHTML += '</table>';

        // for (var i = 0; i < j.length; i++) {
        // options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        // }

        alert('====== el valor de myHTML es:' + myHTML);


        $("#contenedorAdvancedFilterResponse").html(myHTML)
        alert('======= Se esta por mostrar el Contenedor #contenedorSaveClubResponse');
        $('#contenedorSaveClubResponse').show('fast');
        alert('====== Se Termino la funcion.-');
    }
}

function LoginCallBack() {
	 this.execute = function(json,result) {
	 	
	    if (json.response && json.response.data && !json.response.data.pending) {
	    	alert("json.response.message.value: " + json.response.message.value);
	        window.location = json.response.message.value;    
	    }
	    
	    if (json.response.message &&  json.response.message.key == "error") {
	        $("#recuadroMensaje").removeClass("ocultar");
            $("#recuadroMensaje").addClass("alerta");
            $("#recuadroMensaje").html(json.response.message.value);	             
	    }
	    
	    if(result.success == "false") {
	 		$("#recuadroMensaje").removeClass("ocultar");
            $("#recuadroMensaje").addClass("alerta");
	 		$("#recuadroMensaje").html("Intentos excedidos");	  
	 	} 
	 };	 
}

	 
